#include "app_common.h"

long app_common_getcurtime_ms()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}

int app_common_print_hex(char *data, int len)
{
    for (size_t i = 0; i < len; i++)
    {
        printf("%02x ",data[i]);
    }
    printf("\n");
    return 0;
}

char *app_common_bytes2hexstr(char *dta, int len)
{
    char *hexstr = (char *)malloc(len * 2 + 1);

    for (int i = 0; i < len; i++)
    {
        sprintf(hexstr + i * 2, "%02x ", dta[i]);
    }

    hexstr[len * 2] = '\0';
    return hexstr;
}

void app_common_hexstr2bytes(char *hexstr, char *bytes_buff)
{
    for (int i = 0; i < strlen(hexstr)/2; i++)
    {
        // 2 表示一次取两个字节的长度， hh 转换为char , x 来源为16进制  
       sscanf(hexstr + i * 2, "%2hhx", &bytes_buff[i]);
    }
}
